package com.spring.controller;

import com.spring.mapper.UsersMapper;
import com.spring.pojo.Users;
import org.mockito.internal.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/users")
public class UsersController {
	@Autowired
	private UsersMapper usersMapper;

	/**
	 * 页面跳转
	 * 123
	 * 456
	 * 781111
	 */
	@RequestMapping("/{page}")
	public String showPage(@PathVariable String page) {
		return page;
	}

	/**
	 * 添加用户
	 * 1111
     * 2222
	 * 333
	 * 444
	 * 55555
	 */
	@RequestMapping("/addUser")
	public String addUser(Users users) {
		this.usersMapper.insertUsers(users);
		return "ok";
	}

	@RequestMapping("list")
	public String list(Model model, HttpSession session) {
		List<Users> list =usersMapper.selectUsers(new Users()) ;
		model.addAttribute("list",list);
		model.addAttribute("Name",((Users)session.getAttribute("users")).getNam());
		return "list";
	}


	@RequestMapping("login")
	public String login(Model model, Users users, HttpSession session) {
		if (StringUtils.isEmpty(users.getNam()) || StringUtils.isEmpty(users.getPwd())){
			model.addAttribute("error","请输入用户名或密码");
			return "login";
		}
		List<Users> list =usersMapper.selectUsers(users) ;
	if (list !=null && list.size() >0){
		session.setAttribute("users",list.get(0));
		return "redirect:/users/list";
	}
		return "login";
	}
}